﻿using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.ComponentModel.Design;
using Microsoft.VisualStudio.Shell.Interop;

using MsVsShell = Microsoft.VisualStudio.Shell;
using VsConstants = Microsoft.VisualStudio.VSConstants;
using ErrorHandler = Microsoft.VisualStudio.ErrorHandler;

namespace Tcvt.Packages
{
    [Guid("EFD47DA9-9346-4af5-84F9-EADB588FF4FD")]
    public class WorkItemExplorerPane : MsVsShell.ToolWindowPane
    {
		// Control that will be hosted in the tool window
        private WorkItemExplorerUserControl _control = null;
        private TcvtVsPackage _package = null;

        // Caching our output window pane
		//private IVsOutputWindowPane outputWindowPane = null;

		/// <summary>
		/// Constructor for ToolWindowPane.
		/// Initialization that depends on the package or that requires access
		/// to VS services should be done in OnToolWindowCreated.
		/// </summary>
        public WorkItemExplorerPane()
			: base(null)
		{
			Trace.WriteLine(String.Format(CultureInfo.CurrentCulture, "Entering constructor for class {0}.", this.GetType().Name));

			// Set the image that will appear on the tab of the window frame
			// when docked with another window.
			// The resource ID corresponds to the one defined in Resources.resx,
			// while the Index is the offset in the bitmap strip. Each image in
			// the strip is 16x16.
			//this.BitmapResourceID = 301;
			//this.BitmapIndex = 1;

			// Creating the user control that will be displayed in the window
            _control = new WorkItemExplorerUserControl();
		}

		/// <summary>
		/// This property returns the handle to the user control that should
		/// be hosted in the Tool Window.
		/// </summary>
		public override IWin32Window Window
		{
            get { return (IWin32Window)_control; }
		}

		/// <summary>
		/// This is called after our control has been created and sited.
		/// This is a good place to initialize the control with data gathered
		/// from Visual Studio services.
		/// </summary>
		public override void OnToolWindowCreated()
		{
			base.OnToolWindowCreated();

            //_control = new WorkItemExplorerUserControl(_package);
            //_control.TcvtPackage = _package;

            // Set the text that will appear in the title bar of the tool window.
			// Note that because we need access to the package for localization,
			// we have to wait to do this here. If we used a constant string,
			// we could do this in the constructor.
			this.Caption = "Work Item Explorer";
            this.BitmapResourceID = 500;
            this.BitmapIndex = 3;
            ((IVsWindowFrame)this.Frame).SetProperty((int)__VSFPROPID.VSFPROPID_BitmapResource, 500);
            ((IVsWindowFrame)this.Frame).SetProperty((int)__VSFPROPID.VSFPROPID_BitmapIndex, 3);

            // Display the pane as the first tab inside the VS IDE.
            int result = ((IVsWindowFrame)this.Frame).SetProperty((int)__VSFPROPID.VSFPROPID_FrameMode, VSFRAMEMODE.VSFM_MdiChild);
            if (result != VsConstants.S_OK)
            {
                MessageBox.Show("Failed to dock Work Item Explorer under Visual Studio IDE window.", "Work Item Explorer");
            }
		}

        protected override void OnClose()
        {
            base.OnClose();

            _control.Dispose();
            _control = null;
        }

        public TcvtVsPackage TcvtPackage
        {
            get
            {
                return _package;
            }
            set
            {
                _package = value;
                _control.TcvtPackage = _package;
            }
        }
    }
}
